package com.example.ccbb.okhttp;

import android.os.SystemClock;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GetImg {
    private byte[] result = null;
    private String url = "";
    private int temp = 0;

    public GetImg(String url) {
        this.url = url;
    }

    public byte[] getImg() {
        Request request = new Request.Builder().url(url).build();
        OkHttpClient client = new OkHttpClient();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                result = response.body().bytes();
            }
        });
        while (true) {
            if (result != null || temp == 2000) {
                break;
            } else {
                SystemClock.sleep(1);
                temp++;
            }
        }
        return result;
    }
}
